<?php
//======================================================================================
//
// Function: Delete a customer dir for good
//
// Programmer: JKJ
// Date : 2025-05-16
//
// Copyright Reeft A/S (c) - 2025
//======================================================================================
// https://tinyfilemanager.github.io/
// https://tinyfilemanager.github.io/config-sample.txt
//======================================================================================
// General config
//======================================================================================
include "config/config.php";
//======================================================================================
// Set language
//======================================================================================
include "include/set_language.php";
//======================================================================================
// Get input
//======================================================================================
if (isset($_REQUEST["input_string"])) {
$input_string = $_REQUEST["input_string"];
} else {
$input_string = '';
}
//======================================================================================
// Set defaults
//======================================================================================
$currentDate = date('Y-m-d');
$currentTime = date('H:i:s');
$returnCode = '00';
$returnMsg = 'Alles ist gut';
//======================================================================================
// Set header
//======================================================================================
header('Content-Type: application/json;charset=utf-8');
//======================================================================================
// Check content
//======================================================================================
if ( $input_string == '' ) {
$returnCode = '99';
$returnMsg = 'No input string passed to program';
$aryHeader = array();
$aryHeader["returnCode"] = $returnCode;
$aryHeader["returnMsg"] = $returnMsg;
$aryHeader["currentDate"] = $currentDate;
$aryHeader["currentTime"] = $currentTime;
$aryHeader["input_exists"] = $input_exists;
$aryHeader["input_string"] = $input_string;
// Create array and prepare for json encoding
$returnJson["header"] = $aryHeader;
// Paint it black
echo(json_encode($returnJson));
exit;
}
//======================================================================================
// Start me up...
//======================================================================================
$starttime = microtime(true);
$dirToDelete = $input_string;
// Safety check: must contain this file with this exact content
$safetyFile = $dirToDelete . DIRECTORY_SEPARATOR . 'i_must_be_deleted.txt';
$requiredKeyword = 'YES_DELETE_THIS_DIR';
// Check for the required safety file
if (!file_exists($safetyFile)) {
$returnCode = '99';
$returnMsg = 'Safety file not found. Aborting delete operation for safety';
}
if ( $returnCode == '00' ) {
$fileContent = trim(file_get_contents($safetyFile));
if ($fileContent !== $requiredKeyword) {
$returnCode = '99';
$returnMsg = 'Safety file does not contain the correct keyword. Aborting delete operation for safety';
}
}
// Did we pass the safety file check and content?
if ( $returnCode == '00' )
{
if (deleteDir( $dirToDelete )) {
$returnCode = '00';
$returnMsg = 'Directory: ' . $input_string .' deleted successfully';
} else {
if ( $returnCode == '00' ) {
$returnCode = '99';
$returnMsg = 'Failed to delete directory: ' . $input_string;
}
}
}
//======================================================================================
// Calculate response time
//======================================================================================
$endtime = microtime(true);
$response_time = $endtime - $starttime;
$response_time = number_format($response_time, 6, '.', '');
$response_time_raw = number_format($response_time, 6, '.', '');
$response_time = '(' . $response_time . ' seconds)';
$response_time_raw = $response_time_raw;
//======================================================================================
// Create header
//======================================================================================
$aryHeader = array();
$aryHeader["returnCode"] = $returnCode;
$aryHeader["returnMsg"] = $returnMsg;
$aryHeader["currentDate"] = $currentDate;
$aryHeader["currentTime"] = $currentTime;
$aryHeader["input_string"] = $input_string;
$aryHeader["response_sec"] = $response_time;
$aryHeader["response_sec_raw"] = $response_time_raw;
// Create array and prepare for json encoding
$returnJson["header"] = $aryHeader;
//======================================================================================
// Paint it black
//======================================================================================
echo(json_encode($returnJson));
//======================================================================================
// Delete dir
//======================================================================================
function deleteDir($dir) {
global $returnCode, $returnMsg;
if (!file_exists($dir)) {
$returnCode = '99';
$returnMsg = "Directory does not exist: $dir";
return false;
}
if (!is_dir($dir)) {
$returnCode = '99';
$returnMsg = "Not a directory: $dir";
return false;
}
$items = array_diff(scandir($dir), ['.', '..']);
foreach ($items as $item) {
$path = $dir . DIRECTORY_SEPARATOR . $item;
if (is_dir($path)) {
deleteDir($path); // Recursive call
} else {
unlink($path);
}
}
return rmdir($dir);
}
?>